<?php
namespace Admin\Controller;
use Common\Util\GoodsData;
use Common\Util\ShopUtil;

class AjaxController extends AdminController {
    public function get_goods_list(){
        $t_goods = M("goods");
        $map = [];
        $keyword = trim($_POST['keyword']);
        if(isset($keyword) && !empty($keyword)){
            $map['g.goods_sn|g.goods_name'] = array('like',"%$keyword%");
        }
        $page = I ( 'page', 1, 'intval' ); // 默认显示第一页数据
        $row = 50;
        $start = ($page - 1)*$row;
        $goods_data = $t_goods
            ->alias('g')
            ->where($map)
            ->field("g.*")
            ->order("g.goods_order Asc,g.id Asc")
            ->limit($start, $row)
            ->select();
        foreach($goods_data as $key=>$val){
            $goods_data[$key]['goods_jiage_info'] = GoodsData::formatGoodsPrice(0,$val['id']);
        }
        $this->return_json_data_code(1,$goods_data);
    }

    /**
     * 获取仓库商品库存列表
     */
    public function get_goods_list_by_warehouse(){
        $t_stock_inventory_report = M('stock_inventory_report');
        $warehouse_id = $_POST['warehouse_id']?intval($_POST['warehouse_id']):0;
        $keyword = $_POST['keyword']?trim($_POST['keyword']):'';
        $pagesize = $_POST['pagesize']?intval($_POST['pagesize']):1;
        $pagenum = $_POST['pagenum']?intval($_POST['pagenum']):100;
        $startnum = ($pagesize - 1)*$pagenum;
        $st_map['s.business_type'] = 1;
        $st_map['s.warehouse_id'] = $warehouse_id;
        $st_map['s.goods_number'] = ['GT',0];
        if(!empty($keyword)){
            $st_map['g.goods_name'] = ['like',"%$keyword%"];
        }
        $stock_list = $t_stock_inventory_report
            ->alias('s')
            ->join('left join '.C('DB_PREFIX').'goods'.' g on g.id = s.goods_id')
            ->where($st_map)
            ->field("s.*,g.goods_name")
            ->order("s.goods_id Asc")
            ->limit($startnum,$pagenum)
            ->select();

        foreach ($stock_list as $key=>$val){
            $goods_info = GoodsData::getGoodsInfo(['id'=>$val['goods_id']]);
            $stock_list[$key]['goods_name'] = $goods_info['goods_name'];
            $stock_list[$key]['goods_sn'] = $goods_info['goods_sn'];
            $stock_list[$key]['goods_qpb_endday_str'] = $val['goods_qpb_endday']?date("Y-m-d",$val['goods_qpb_endday']):'-';
        }
        $this->return_json_data_code(1,$stock_list?$stock_list:[]);
    }
}
?>